package it.polimi.digitalstate.webbeans;

import it.polimi.digitalstate.dto.ElectionVoteDTO;
import it.polimi.digitalstate.dto.JoinrequestDTO;
import it.polimi.digitalstate.dto.MembershipDTO;
import it.polimi.digitalstate.dto.PartyDTO;
import it.polimi.digitalstate.manager.AuthenticationManagerLocal;
import it.polimi.digitalstate.manager.ElectionManagerLocal;
import it.polimi.digitalstate.manager.MembershipManagerLocal;
import it.polimi.digitalstate.manager.PartyManagerLocal;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;

import managerexception.CannotSendElectionVote;
import managerexception.CannotSendJoinRequest;

@ManagedBean(name="elePartyViewBean")
@ViewScoped
public class ElePartyViewBean {
	@EJB
	private PartyManagerLocal ejb;
	@EJB
	private AuthenticationManagerLocal ejb_user;
	@EJB
	ElectionManagerLocal ejb_electionvote;
	private PartyDTO party;
	
	public class EleVote {
		private MembershipDTO member;
		private int priority;
		
/*		public EleVote()
		{
			member = null;
			priority = 0;
		}
		public EleVote (EleVote eleVotes)
		{
			this.member = eleVotes.member;
			this.priority = eleVotes.priority;
		}
*/		public int getPriority() {
			return priority;
		}
		public void setPriority(int priority) {
			this.priority = priority;
		}
		public MembershipDTO getMember() {
			return member;
		}
		public void setMember(MembershipDTO member) {
			this.member = member;
		}
	}
	
	private EleVote[] eleVotes;
	
//	@ManagedProperty("#{param.partyname}")
	private String partyname;
	private static int zero=0, one=1, two= 2, three= 3;


	public String getPartyname() {
		return partyname;
	}
	public void setPartyname(String partyname) {
		this.partyname = partyname;
	}
	
		
	@PostConstruct
	public void init(){

//		System.out.println("partyname: " + partyname);
		if (partyname != null){
					
			party=ejb.getPartyByName(partyname);
			MembershipDTO[] listMembers;
			listMembers = ejb.getListOfMember(party);
			System.out.println("N OF MEMBER " + listMembers.length);
			eleVotes = new EleVote[listMembers.length];
			for(int j=0; j< listMembers.length; j++)
			{
				eleVotes[j] = new EleVote();
				eleVotes[j].member = listMembers[j];
				eleVotes[j].priority = 0;
			}
	//		System.out.println("N OF MEMBER " + eleVotes.length);
		}
	}
	public EleVote[] getEleVotes() {
		return this.eleVotes;
	}
	public void setEleVotes(EleVote[] eleVotes) {
		this.eleVotes = eleVotes;
	}
	public PartyDTO getParty() {
		return party;
	}
	public void setParty(PartyDTO party) {
		this.party = party;
	}

	public int getZero () {
		return zero;
	}
	public int getOne () {
		return one;
	}
	public int getTwo () {
		return two;
	}
	public int getThree () {
		return three;
	}
	
	public String submit() {
		System.out.println("Submit election");
// logic on priority
		boolean error=false;
		String Error_msg = "";
		int P1=-1, P2=-1, P3=-1;
		for (int j=0; j< eleVotes.length; j++)
		{
			if(eleVotes[j].priority == 1)
				if(P1 == -1)
					P1 = j;
				else
					{
						error = true;
						Error_msg = "Only one Member should have priority 1";
					}
		}
		if (P1 == -1)
		{
			error = true;
			Error_msg = "You have to input priority 1 at least.";
		}
		else
		{
			for (int j=0; j< eleVotes.length; j++)
			{
				if(eleVotes[j].priority == 2)
					if(P2 == -1)
						P2 = j;
					else
						{
							error = true;
							Error_msg = "Only one Member should have priority 2";
						}
			}
			if((P2 != -1) && ( P1 == -1))
			{
				error = true;
				Error_msg = "Priorities have to assigned from higher order. Put priority 1 before 2";
			}
			else
			{
				for (int j=0; j< eleVotes.length; j++)
				{
					if(eleVotes[j].priority == 3)
						if(P3 == -1)
							P3 = j;
						else
							{
								error = true;
								Error_msg = "Only one Member should have priority 3";
							}
				}
				if((P3 != -1) && (P2 == -1))
				{
					error = true;
					Error_msg = "Priorities have to assigned from higher order. Put priority 2 before 3";
				}
			}
		}
		
		if (error == true)
		{
			System.out.println(Error_msg);
			return "electionmemberlist.xhtml?faces-redirect=true&partyname=" + partyname;
		}
		else
			{
				if(!ejb_user.isLogged())
				{
					Error_msg = "You need to log into the system.";
					System.out.println(Error_msg);
					return "/login.xhtml?faces-redirect=true";
					
				}
				else if ( partyname=="Orange" /*!canVote(ejb_user.getLoggedCitizen())*/)
				{
					Error_msg = "You have already voted in this election.";
					System.out.println(Error_msg);
					return "/citizen/personalpage.xhtml?faces-redirect=true";
				}
				else
					return sendVote(P1, P2, P3);			
			}
	}
	
	public String sendVote(int p1, int p2, int p3){
		ElectionVoteDTO ev = new ElectionVoteDTO(ejb_user.getLoggedCitizen(), party,
				eleVotes[p1].member.getCitizen(), (p2 == -1) ? null : eleVotes[p2].member.getCitizen(), (p3 == -1) ? null : eleVotes[p3].member.getCitizen());
		try {
			ejb_electionvote.submitElectionVote(ev);
		} catch (CannotSendElectionVote e) {
			return "electionvoteerror?faces-redirect=true";
		}
		return "electionvotesubmitted.xhtml?faces-redirect=true";
	}

}
